package com.csun.cmny.provider.controller;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.provider.dao.OmcBillAdjustFeeMapper;
import com.csun.cmny.provider.dao.OmcOldManBillMapper;
import com.csun.cmny.provider.dto.DoctorWorkMedicalAdviceDownDto;
import com.csun.cmny.provider.dto.OmcOldManBillDto;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcBillAdjustFee;
import com.csun.cmny.provider.model.domain.OmcOldManBill;
import com.csun.cmny.provider.model.domain.OmcSanitationService;
import com.csun.cmny.provider.model.dto.disinfection.DisinfectionRecordDto;
import com.csun.cmny.provider.model.dto.disinfection.HuaLongToiletDownDto;
import com.csun.cmny.provider.model.dto.doctor.HuaLongBloodPressureDownDto;
import com.csun.cmny.provider.model.dto.doctor.HuaLongTemperatureDownDto;
import com.csun.cmny.provider.model.dto.nuringTour.HuaLongTourDownDto;
import com.csun.cmny.provider.model.vo.revise.ReviseVo;
import com.csun.cmny.provider.model.vo.disinfection.HuaLongToiletDsftRecordVo;
import com.csun.cmny.provider.model.vo.disinfection.OmcDisinfectionRecordVo;
import com.csun.cmny.provider.model.vo.disinfectionPosition.HuaLongEveryDsftMethodVo;
import com.csun.cmny.provider.model.vo.disinfectionPosition.HuaLongEveryDsftRecordVo;
import com.csun.cmny.provider.model.vo.doctor.HuaLongBloodPressureRecordVo;
import com.csun.cmny.provider.model.vo.doctor.HuaLongTemperatureRecordVo;
import com.csun.cmny.provider.model.vo.hualong.HuaLongRosterVo;
import com.csun.cmny.provider.model.vo.nursingRecords.HuaLongTourRecordVo;
import com.csun.cmny.provider.model.vo.old_out.OldOutVo;
import com.csun.cmny.provider.model.vo.sanitation.HuaLongSanitationVo;
import com.csun.cmny.provider.pojo.Oldman;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.utils.DateUtil;
import com.csun.cmny.provider.utils.JxlExcelUtils;
import com.csun.cmny.provider.vo.DoctorWorkMedicalAdviceDownVo;
import com.csun.cmny.provider.vo.OmcOldManBillDetailVo;
import com.csun.cmny.util.PublicUtil;
import com.google.common.collect.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-11-11 09:54
 * @Description:
 */
@RestController
@RequestMapping(value = "/auth")
public class AuthRestController {

    @Resource
    private OmcHuaLongRosterService omcHuaLongRosterService;
    @Resource
    private OmcOldmanSanitationRecordService omcOldmanSanitationRecordService;
    @Resource
    private OmcSanitationServiceService omcSanitationServiceService;
    @Resource
    private OmcEverydayDisinfectionRecordService omcEverydayDisinfectionRecordService;
    @Resource
    private OmcDisinfectionMethodService omcDisinfectionMethodService;
    @Resource
    private OmcCMDisinfectionService omcCMDisinfectionService;
    @Resource
    private OmcNursingTourRecordService omcNursingTourRecordService;
    @Resource
    private ChongmingDoctorWorkService chongmingDoctorWorkService;
    @Resource
    private OmcOldManBillService omcOldManBillService;
    @Resource
    private OmcOldManBillMapper omcOldManBillMapper;
    @Resource
    private OmcOldOutService omcOldOutService;
    @Resource
    private OmcReviseService omcReviseService;
    @Resource
    private OmcBillAdjustFeeMapper omcBillAdjustFeeMapper;
    @Resource
    private OmcOldmanExpenseItemService omcOldmanExpenseItemService;
    @Resource
    private OldmanService oldmanService;

    /**
     * 根据排班Id导出排班表
     *
     * @param response
     *
     * @param rosterId
     */
    @RequestMapping("/hualong/roster/down")
    public void rosterDown(HttpServletResponse response, Long rosterId) {

        HuaLongRosterVo huaLongRosterVo = omcHuaLongRosterService.queryDetailById(rosterId);


        String filename = "上海崇明区福利院护理部排班表";
        String titleName = String.format("上海崇明区福利院护理部排班表(%s人)", huaLongRosterVo.getNurseNum());
        String sheetName = "上海崇明区福利院护理部排班表";
        List<String> columns = Lists.newArrayList();

        int day = DateUtil.getDayOfMonth(huaLongRosterVo.getMonth());
        // 查询指定月份的天数
        columns.add("日期");
        for (int i = 1; i <= day; i++) {
            columns.add(i + "");
        }

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            JxlExcelUtils.rosterDown(response, huaLongRosterVo, titleName, sheetName, columns);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据老人和日期导出排班表
     *
     * @param response
     * @param elderId   老人Id
     * @param month     月份 yyyy-MM
     */
    @RequestMapping("/hualong/sanitation/down")
    public void sanitationDown(HttpServletResponse response, Integer elderId, String month, Long groupId) {

        List<HuaLongSanitationVo> huaLongSanitationVoList = omcOldmanSanitationRecordService
                .queryElderAndMonth(elderId, month);

        String filename = "老人照护(个人卫生)记录表";
        String titleName = "老人照护(个人卫生)记录表";
        String sheetName = "老人照护(个人卫生)记录表";

        // 查询护理项目列表
        OmcSanitationService omcSanitationService = new OmcSanitationService();
        omcSanitationService.setGroupId(groupId);
        List<OmcSanitationService> omcSanitationServiceList = omcSanitationServiceService.select(omcSanitationService);

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            JxlExcelUtils.sanitationDown(response, huaLongSanitationVoList, titleName, sheetName, omcSanitationServiceList, month);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 根据楼栋和日期导出排班表
     *
     * @param response
     * @param buildingNo    楼栋编号
     * @param month         月份 yyyy-MM
     */
    @RequestMapping("/hualong/dsft/down")
    public void dsftDown(HttpServletResponse response, String buildingNo, String month, Long groupId) {

        List<HuaLongEveryDsftRecordVo> huaLongEveryDsftRecordVoList = omcEverydayDisinfectionRecordService
                .queryBuildingNoAndMonth(buildingNo, month);

        String filename = "日常用品消毒记录表";
        String titleName = "日常用品消毒记录表";
        String sheetName = "日常用品消毒记录表";

        // 查询消毒方式
        List<HuaLongEveryDsftMethodVo> huaLongEveryDsftMethodVoList = omcDisinfectionMethodService
                .queryByGroupId(groupId);

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            JxlExcelUtils.everyDsftDown(response, huaLongEveryDsftRecordVoList, titleName, sheetName, huaLongEveryDsftMethodVoList, month, buildingNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 便器消毒记录导出
     *
     * @param response
     * @param huaLongToiletDownDto
     */
    @RequestMapping("/hualong/toilet/down")
    public void toiletDown(HttpServletResponse response, HuaLongToiletDownDto huaLongToiletDownDto) {

        List<HuaLongToiletDsftRecordVo> huaLongToiletDsftRecordVoList = omcCMDisinfectionService
                .queryToiletDsftRecord(huaLongToiletDownDto);

        String groupName = "";
        if (PublicUtil.isNotEmpty(huaLongToiletDownDto.getGroupName())) {
            groupName = huaLongToiletDownDto.getGroupName();
        }

        String filename =  groupName + "便器消毒记录";
        String titleName = groupName + "便器消毒记录";
        String sheetName = groupName + "便器消毒记录";

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            JxlExcelUtils.toiletDsftDown(response, huaLongToiletDsftRecordVoList, titleName, sheetName, huaLongToiletDownDto);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 巡视记录报表导出（机构, 时间）
     *
     * @param response
     *
     * @param huaLongTourDownDto
     */
    @RequestMapping("/hualong/tour/down")
    public void tourDown(HttpServletResponse response, HuaLongTourDownDto huaLongTourDownDto) {

        List<HuaLongTourRecordVo> huaLongTourRecordVoList = omcNursingTourRecordService
                .queryTourRecord(huaLongTourDownDto);

        String filename = "巡视记录表";
        String titleName = "巡视记录表";
        String sheetName = "巡视记录表";

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            JxlExcelUtils.tourDown(response, huaLongTourRecordVoList, titleName, sheetName, huaLongTourDownDto);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 体温报表导出（老人Id, 时间）
     *
     * @param response
     *
     * @param huaLongTemperatureDownDto
     */
    @RequestMapping("/hualong/temperature/down")
    public void temperatureDown(HttpServletResponse response, HuaLongTemperatureDownDto huaLongTemperatureDownDto) {

        List<HuaLongTemperatureRecordVo> huaLongTemperatureRecordVoList = chongmingDoctorWorkService
                .queryTemperatureRecord(huaLongTemperatureDownDto);

        String filename = "体温记录表";
        String titleName = "体温记录表";
        String sheetName = "体温记录表";

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            JxlExcelUtils.temperatureDown(response, huaLongTemperatureRecordVoList, titleName, sheetName, huaLongTemperatureDownDto);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 血压报表导出（老人Id, 时间）
     *
     * @param response
     *
     * @param huaLongBloodPressureDownDto
     */
    @RequestMapping("/hualong/bloodPressure/down")
    public void bloodpressureDown(HttpServletResponse response, HuaLongBloodPressureDownDto huaLongBloodPressureDownDto) {

        List<HuaLongBloodPressureRecordVo> huaLongBloodPressureRecordVoList = chongmingDoctorWorkService
                .queryBloodPressureRecord(huaLongBloodPressureDownDto);

        String filename = "血压记录表";
        String titleName = "血压记录表";
        String sheetName = "血压记录表";

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            JxlExcelUtils.bloodPressureDown(response, huaLongBloodPressureRecordVoList, titleName, sheetName, huaLongBloodPressureDownDto);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/hualong/elevator/down")
    public void elevatorDown(HttpServletResponse response, DisinfectionRecordDto disinfectionRecordDto) {

        disinfectionRecordDto.setPageSize(100);
        List<OmcDisinfectionRecordVo> omcDisinfectionRecordVos = omcCMDisinfectionService
                .selectRecord(disinfectionRecordDto);


        String prefixName = "";
        if (PublicUtil.isNotEmpty(omcDisinfectionRecordVos)) {
            OmcDisinfectionRecordVo omcDisinfectionRecordVo = omcDisinfectionRecordVos.get(0);

            prefixName = omcDisinfectionRecordVo.getDisinfectionDate().substring(0,7)+" "+
                    omcDisinfectionRecordVo.getBuildName()+" "+
                    omcDisinfectionRecordVo.getServiceItemNo()+" ";
        }

        String filename =  prefixName + "电梯消毒记录";
        String titleName = prefixName + "电梯消毒记录";
        String sheetName = prefixName + "电梯消毒记录";

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            JxlExcelUtils.elevatorDsftDown(response, omcDisinfectionRecordVos, titleName, sheetName, disinfectionRecordDto);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/hualong/diningCar/down")
    public void diningCarDown(HttpServletResponse response, DisinfectionRecordDto disinfectionRecordDto) {

        disinfectionRecordDto.setPageSize(100);
        List<OmcDisinfectionRecordVo> omcDisinfectionRecordVos = omcCMDisinfectionService
                .selectRecord(disinfectionRecordDto);

        String prefixName = "";
        if (PublicUtil.isNotEmpty(omcDisinfectionRecordVos)) {
            OmcDisinfectionRecordVo omcDisinfectionRecordVo = omcDisinfectionRecordVos.get(0);

            prefixName = omcDisinfectionRecordVo.getDisinfectionDate().substring(0,7)+" "+
                    omcDisinfectionRecordVo.getBuildName()+" ";
        }

        String filename =  prefixName + "送餐车消毒记录";
        String titleName = prefixName + "送餐车消毒记录";
        String sheetName = prefixName + "送餐车消毒记录";

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            JxlExcelUtils.diningCarDsftDown(response, omcDisinfectionRecordVos, titleName, sheetName, disinfectionRecordDto);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Auther: ZNR
     * @Date: 2020-11-19
     * @Description:
     */
    /**
     * 长期医嘱单报表导出（老人Id, 时间）
     *
     * @param response
     *
     * @param doctorWorkMedicalAdviceDownDto
     */
    @RequestMapping("/hualong/medicalAdvice/down")
    public void medicalAdviceDown(HttpServletResponse response, DoctorWorkMedicalAdviceDownDto doctorWorkMedicalAdviceDownDto) {

        List<DoctorWorkMedicalAdviceDownVo> doctorWorkMedicalAdviceDownVoList = chongmingDoctorWorkService
                .queryMedicalAdvice(doctorWorkMedicalAdviceDownDto);

        String filename = "长期医嘱单";
        String titleName = "长期医嘱单";
        String sheetName = "长期医嘱单";

        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            JxlExcelUtils.medicalAdviceDown(response, doctorWorkMedicalAdviceDownVoList, titleName, sheetName, doctorWorkMedicalAdviceDownDto);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Auther: ZNR
     * @Date: 2020-12-17
     * @Description:
     */
    /**
     * 入院账单报表导出（订单ID）
     * @param response
     * @param id
     */
    @RequestMapping("/dfc/checkInBill/down")
    public void checkInBillDown(HttpServletResponse response,Long id) {
        OmcOldManBill omcOldManBill = omcOldManBillMapper.selectByPrimaryKey(id);
        OmcOldManBillDto omcOldManBillDto = new OmcOldManBillDto();
        omcOldManBillDto.setBillId(id);
        OmcOldManBillDetailVo omcOldManBillDetailVo = omcOldManBillService.queryBillDetail(omcOldManBillDto);

        String filename = "入院账单";
        String titleName = "入院办理费用结算详情";
        String sheetName = "入院账单";
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            JxlExcelUtils.checkInBillDown(response, omcOldManBill, titleName, sheetName, omcOldManBillDetailVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Auther: ZNR
     * @Date: 2020-12-18
     * @Description:
     */
    /**
     * 出院账单报表导出（订单ID）
     * @param response
     * @param id
     */
    @RequestMapping("/dfc/checkOutBill/down")
    public void checkOutBillDown(HttpServletResponse response,Long id) {
        OmcOldManBill omcOldManBill = omcOldManBillMapper.selectByPrimaryKey(id);
        OldOutVo oldOutVo = omcOldOutService.queryByBillId(id);

        String filename = "出院账单";
        String titleName = "出院退费费用结算详情";
        String sheetName = "出院账单";
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            JxlExcelUtils.checkOutBillDown(response, titleName, sheetName, omcOldManBill, oldOutVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Auther: ZNR
     * @Date: 2020-12-21
     * @Description:
     */
    /**
     * 调整账单报表导出（订单ID）
     * @param response
     * @param id
     */
    @RequestMapping("/dfc/reviseBill/down")
    public void reviseBillDown(HttpServletResponse response,Long id) {
        OmcOldManBill omcOldManBill = omcOldManBillService.selectByKey(id);
        ReviseVo reviseVo = omcReviseService.queryByBillId(id);
        OmcBillAdjustFee omcBillAdjustFee = new OmcBillAdjustFee();
        omcBillAdjustFee.setBillId(id);
        omcBillAdjustFee = omcBillAdjustFeeMapper.selectOne(omcBillAdjustFee);
        Oldman oldman = new Oldman();
        oldman.setOldmanId(omcOldManBill.getOldManId().intValue());
        Oldman old = oldmanService.selectByOld(oldman);
        if (PublicUtil.isEmpty(old)){
            throw new OmcBizException(ErrorCodeEnum.OMC10038002);
        }

        String filename = "调整账单";
        String titleName = "调整账单费用结算详情";
        String sheetName = "调整账单";
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            JxlExcelUtils.reviseBillDown(response, titleName, sheetName, omcOldManBill, reviseVo, omcBillAdjustFee, old.getSex());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Auther: ZNR
     * @Date: 2020-12-22
     * @Description:
     */
    /**
     * 周期账单报表导出（订单ID）
     * @param response
     * @param id
     */
    @RequestMapping("/dfc/cycleBill/down")
    public void cycleBillDown(HttpServletResponse response,Long id) {
        OmcOldManBillDto omcOldManBillDto = new OmcOldManBillDto();
        omcOldManBillDto.setBillId(id);
        OmcOldManBillDetailVo omcOldManBillDetailVo = omcOldManBillService.queryBillDetail(omcOldManBillDto);
        OmcOldManBill omcOldManBill = omcOldManBillMapper.selectByPrimaryKey(id);


        String filename = "周期账单";
        String titleName = "周期账单费用结算详情";
        String sheetName = "周期账单";
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(filename.getBytes("UTF-8"),"ISO8859-1") + ".xls");

            JxlExcelUtils.cycleBillDown(response, titleName, sheetName, omcOldManBill, omcOldManBillDetailVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test() {

        int day = DateUtil.getDayOfMonth("2020-11");

        System.out.println("day = " + day);

        System.out.println((char)8730);
    }
}
